import { Button, Card, Form, Input, Checkbox, message } from 'antd';
import logo from 'assets/logo.png';
import './index.scss';
import { useDispatch } from 'react-redux';
import { useHistory } from 'react-router-dom';
import { login } from 'store/actions';
const Login = () => {
  const dispatch = useDispatch();
  const history = useHistory();
  const onFinish = async (values) => {
    const { mobile, code } = values;
    try {
      await dispatch(login(mobile, code));
      history.replace('/home');
      message.success('登录成功');
    } catch (e) {
      message.error(e.response?.data?.message || '登录失败');
    }
  };
  return (
    <div className="login">
      <Card className="login-container">
        <img className="login-logo" src={logo} alt="" />
        {/* 表单 */}
        <Form
          name="basic"
          size="large"
          validateTrigger={['onChange', 'onBlur']}
          onFinish={onFinish}
          initialValues={{
            agree: true,
            mobile: '13911111111',
            code: '246810',
          }}
        >
          <Form.Item
            name="mobile"
            rules={[
              { pattern: /^1[3-9]\d{9}$/, message: '手机号码格式不对' },
              { required: true, message: '请输入手机号' },
            ]}
          >
            <Input placeholder="请输入手机号" />
          </Form.Item>
          <Form.Item
            name="code"
            rules={[
              { pattern: /^\d{6}$/, message: '验证码格式不对' },
              { required: true, message: '请输入验证码' },
            ]}
          >
            <Input placeholder="请输入验证码" />
          </Form.Item>
          <Form.Item
            name="agree"
            valuePropName="checked"
            rules={[{ required: true, message: '请阅读协议' }]}
          >
            <Checkbox>我已阅读并同意[用户协议]和[隐私条款]</Checkbox>
          </Form.Item>
          <Form.Item name="password">
            <Button type="primary" htmlType="submit" block>
              登录
            </Button>
          </Form.Item>
        </Form>
      </Card>
    </div>
  );
};
export default Login;
